package com.github.wxiaoqi.security.biz.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.biz.BhReceiveBiz;
import com.github.wxiaoqi.security.biz.biz.BhTaskBiz;
import com.github.wxiaoqi.security.biz.entity.BhReceive;
import com.github.wxiaoqi.security.biz.entity.BhTask;
import com.github.wxiaoqi.security.biz.entity.BhTaskReceive;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("bhTaskReceiver")
public class ReceiverTaskAction {
	@Autowired
	private BhReceiveBiz bhReceiveBiz;
	@Autowired
	private BhTaskBiz bhTaskBiz;
	/**
	 * 领取任务
	 * custNo,taskId
	 */
	@RequestMapping(value = "/receiver", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> receiver(@RequestBody Map<String, Object> reqs) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			
			BhReceive receive =  EntityUtils.map2Bean(reqs, BhReceive.class);
			//判断任务是否存在
			BhTask task = bhTaskBiz.selectById(receive.getTaskId());
			if(task == null){
				result.put("code", ResponseCode.TASK_NOTEXIST.getCode());
				result.put("sucess", false);
				result.put("msg", ResponseCode.TASK_NOTEXIST.getMessage());
				log.info("领取任务结束{}", result);
				return result;
			}
			receive.setCreatetime(new Date());
			Long id = SnowFlake.getId();
			receive.setId(String.valueOf(id));
			//不能领取自己发布的任务
			//用户架构未定暂时跳过
			//
			//查询任务是否重复领取
			BhReceive receiveTest = new BhReceive();
			receiveTest.setTaskId(receive.getTaskId());
			receiveTest.setCustId(receive.getCustId());
			Long count = bhReceiveBiz.selectCount(receiveTest);
			if(count > 0){
				result.put("code", ResponseCode.REPEAT_RECEIVER.getCode());
				result.put("sucess", false);
				result.put("msg", ResponseCode.REPEAT_RECEIVER.getMessage());
				log.info("领取任务结束{}", result);
				return result;
			}
			bhReceiveBiz.insert(receive);
			result.put("code", ResponseCode.OK.getCode());
			result.put("sucess", true);
			result.put("msg", ResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
		}
		log.info("领取任务结束{}", result);
		return result;
	}
	/**
	 * 领取的任务列表
	 */
	@RequestMapping(value = "/receiverTaskList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> receiverTaskList(@RequestBody Map<String, Object> req) {
		log.info("领取的任务列表传入参数{}", req);
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> reqs = new HashMap<String, Object>();
		reqs.putAll(req);
		try {
			
			TableResultResponse<BhTaskReceive> response = bhReceiveBiz.receiveList(reqs);
			result.put("body", response);
			result.put("code", ResponseCode.OK.getCode());
			result.put("sucess", true);
			result.put("msg", ResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
		}
		log.info("领取的任务列表结束{}", result);
		return result;
	}
 }
